/**
 * @discript 选择排序 O(n^2)
 *
 *
 *
 */
function findTarget(arr) {
  let minItem = arr[0]; //存储最小值
  let minItemIndex = 0; //存储最小索引
  let len = arr.length;
  for (let i = 0; i < len; i++) {
    const item = arr[i];
    if (item < minItem) {
      minItem = item;
      minItemIndex = i;
      return minItemIndex;
    }
  }
}

function selectionSort(arr) {
  let newArr = [];
  // 找出数组中最小的
  let len = arr.length;
  for (let i = 0; i < len; i++) {
    // 找出数组中最小项索引
    const minIndex = findTarget(arr);
    // 将最小的存储新数组
    const minItem = arr.splice(minIndex, 1);
    newArr.unshift(minItem);
  }

  return newArr.flat();
}
console.log(selectionSort([5, 3, 6, 2, 10]));
